#!/usr/bin/python


import os
import nltk
import sys
import string

#from nltk.corpus import gutenberg, genesis, inaugural,\
#       nps_chat, webtext, treebank, wordnet
#from nltk.text import Text
from nltk.probability import FreqDist
#from nltk.util import bigrams
from nltk.corpus import stopwords
from  nltk.corpus import wordnet as wn
#from decimal import digit





usage = 'first.py keyword '
found = 0
result_set = []
synonyms = []
contents =[]
Glossary_terms = []
glossary_synonyms =[]



def syn(text):
    print text
    synonyms_list = []
    for synset in wn.synsets(text):
        synonyms_list = synonyms_list + synset.lemma_names
    #print "wordnet result is = ", synonyms_list[1:]
    return synonyms_list
    
def refine(list):
    contents = [ch for ch in list if ch not in string.punctuation 
                    and ch.lower() not in stopwords 
                    and ch.lower() not in references_extra 
                    and not ch.isdigit()
                    and len(ch)> 2]

args = sys.argv[1:]
if len(args) != 1:
        print "Unrecognized arguments %s, %s.\nUsage: %s" % (args.pop(0),args.pop(0) , usage )
        sys.exit(1)


keyword = args.pop(0)
#keyword = "acceleration"
keyword = keyword.lower()
print "the input is = ",  keyword

try:
    synonyms = syn(keyword)
except Exception, err1:
    print err1


try:
    #os.listdir('.')
    doc = open('/home/venky/Documents/GR_Project/glossary.txt', 'rU')
except Exception, err:
    print err


for line in doc:
    if line.find(keyword)>-1:
        definition = line
        print "definition found in glossary :)"
        print definition
        tokens = nltk.word_tokenize(line)
        stopwords = nltk.corpus.stopwords.words('english')
        #print tokens[1:]
        contents = []
        references_extra = ["chap.","sec.","chap","sec","p"]
        contents = [ch for ch in tokens if ch not in string.punctuation 
                    and ch.lower() not in stopwords 
                    and ch.lower() not in references_extra 
                    and not ch.isdigit()
                    and len(ch)> 2]
        
        Glossary_terms = Glossary_terms + contents
    else:
        found = 0
 
for w in Glossary_terms:
    glossary_synonyms = glossary_synonyms + syn(w)
#print "glossary synonyms = ", glossary_synonyms
 

result_set = glossary_synonyms + synonyms
#print "final set =  ", result_set

freq_words = FreqDist(result_set)
#print freq_words
result_set = freq_words.keys()
print "final result = ", result_set[:15]
